package it.yuzuojian.com.controller;

import com.github.pagehelper.PageInfo;
import it.yuzuojian.com.javaBean.Article;
import it.yuzuojian.com.javaBean.Comment1;
import it.yuzuojian.com.javaBean.Tags;
import it.yuzuojian.com.javaBean.Type;
import it.yuzuojian.com.service.*;
import it.yuzuojian.com.util.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class IndexController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleTagsService articleTagsService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagsService tagsService;
    @Autowired
    private Comment1Service comment1Service;
    @GetMapping("")
    public String index_null(){
        return "redirect:/index";
    }
    @GetMapping("/index")
    public String index(Model model,
                        @RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "pageSize", defaultValue = "4") int pageSize){
        List<Type> typeList = typeService.findAllCount();
        List<Tags> tagsList = tagsService.findAllCount();
        List<Article> articles = articleService.findAll(page, pageSize);
        List<Article> recommendNewArticleList=articleService.findRecommendNewArticle();
        PageInfo<Article> pageInfo=new PageInfo<>(articles);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(page);
        /*Integer articleCounts=articleService.findCounts();*/

        model.addAttribute("typeList",typeList);
        model.addAttribute("tagsList",tagsList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("recommendNewArticleList",recommendNewArticleList);
//        model.addAttribute("articleCounts",articleCounts);
        return "index";
    }







    @GetMapping("/about")
    public String about(){
        return "/about";
    }

    @GetMapping("/{typeName}/types")
    public String type_Article(@PathVariable("typeName") String typeName,
                               @RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "pageSize", defaultValue = "3") int pageSize,
                               Model model){
        List<Type> types = typeService.findAll();
        List<Article> articles = articleService.findByTypeName(page,pageSize,typeName);
        PageInfo<Article> pageInfo=new PageInfo<>(articles);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("activeTypeName",typeName);
        model.addAttribute("types",types);
        return "types";
    }

    @GetMapping("/tags/{id}")
    public String tags_Article(@PathVariable("id") Integer id,
                               @RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "pageSize", defaultValue = "3") int pageSize,
                               Model model){
        List<Tags> tags = tagsService.findAll();
        List<Article> articles = articleService.findByTagId(page,pageSize,id);
        PageInfo<Article> pageInfo=new PageInfo<>(articles);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("activeId",id);
        model.addAttribute("tags",tags);
        return "tags";
    }

    @GetMapping("/archives")
    public String archives(Model model,
                           @RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "pageSize", defaultValue = "6") int pageSize){
        Map<String,List<Article>> archiveMap = new HashMap<>();
        List<Article> articleList = articleService.findAll(page,pageSize);
        PageInfo<Article> pageInfo=new PageInfo<>(articleList);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
        for (Article article : articleList) {
            archiveMap.put(simpleDateFormat.format(article.getCreateTime()),new ArrayList<Article>());
        }
        for (Article article : articleList) {
            archiveMap.forEach((key,value) ->{
                if(key.equals(simpleDateFormat.format(article.getCreateTime()))){
                    value.add(article);
                }
            });
        }

        model.addAttribute("archiveMap",archiveMap);
        model.addAttribute("pageInfo",pageInfo);
//        model.addAttribute("counts",articleService.findCounts());

        return "archives";

    }


    @GetMapping("/article/{id}")
    public String articleInfo(@PathVariable("id") Integer id,Model model){
        Article article = articleService.findById(id);
        String content = MarkdownUtils.markdownToHtmlExtensions(article.getContent());
        article.setContent(content);
        articleService.addViewCount(id);
        model.addAttribute("article",article);
        return "articleInfo";
    }
    @GetMapping("/comments/{articleId}")
    public String comments(Model model,@PathVariable("articleId") Integer articleId){
        Map<Comment1,List<Comment1>> comments= new HashMap<>();
        List<Comment1> comment1s = comment1Service.findByArticleId(articleId);
        for (Comment1 comment1 : comment1s) {
            if(comment1.getPId()==-1){
                comments.put(comment1,new ArrayList<>());
            }
        }
        for (Comment1 comment1 : comment1s) {
            comments.forEach((key,value) ->{
                if(key.getId()==comment1.getGId()){
                    value.add(comment1);
                }
            });
        }
        model.addAttribute("comments",comments);
        System.out.println(comments);
        return "articleInfo :: commentList";
    }

//    @GetMapping("article/addViewCount/{id}")
//    public String addViewCount(Integer id){
//        articleService.addViewCount(id);
//        return
//    }
    @GetMapping("/search")
    public String search(@RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                         String query, Model model){
        String query1="%"+query+"%";
        List<Article> articles = articleService.search(page, pageSize, query1);
        PageInfo<Article> pageInfo=new PageInfo<>(articles);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(page);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("query",query);
        return "search";
    }


}
